const { Op } = require("sequelize");
const peopleModel = require("../model/people")

class PeopleService {
    // 查询所有人员信息,不包括已删除的
    async findAll() {
        return await peopleModel.findAll({
            where: {
                isDelete: false
            }
        });
    }

    // 根据姓名查询人员信息
    async findByName(name) {
        return await peopleModel.findAll({
            where: {
                name: {
                    [Op.like]: `%${name}%`
                },
                isDelete: false
            }
        });
    }

    // 分页查询,不包括已删除的
    async findPage(page, size) {
        return await peopleModel.findAndCountAll({
            offset: (page - 1) * size * 1,
            limit: size * 1,
            where: {
                isDelete: false
            }
        });
    }
    // 新增人员
    async createPeopleService({ name, sex, birth, phone, political, education, department, position, entryTime, status }) {
        return await peopleModel.create({ name, sex, birth, phone, political, education, department, position, entryTime, status });
    }
    // 删除人员
    async deletePeople(id) {
        return await peopleModel.update({ isDelete: true }, {
            where: {
                id
            }
        });
    }
    // 根据id查询人员信息 不包括已删除的
    async findById(id) {
        return await peopleModel.findAll(
            {
                where: {
                    id,
                    isDelete: false
                }
            }
        );
    }
}

module.exports = PeopleService;
